import {API} from '../api/index.js'


/**
 * 获取用户购物车列表
 */
const _getUserCart = function() {
    const url = "/mobile/cart/getUserCart";
    let user=JSON.parse(localStorage.getItem("user"))
    const result = API.post(url,{uid:user.user_id});
    return result;
};


/**
 * 添加购物车商品
 * @param {*} goodid 
 */
const _addCartGoods = function(goodid) {
    const url = "/mobile/cart/addCartGoods";
    let user=JSON.parse(localStorage.getItem("user"))
    const result = API.post(url,{token:user.token,uid:user.user_id,goods_id:goodid});
    return result;
};

/**
 * 修改购物车商品数量
 * @param {*} num 
 */
const _changeCartGoodsNum = function(param) {
    const url = "/mobile/cart/changeCartGoodsNum";
    const result = API.post(url,param);
    return result;
};


/**
 * 删除购物车商品
 * @param {*} id [1,2,3,4,5,6]
 */
const _cartDel = function(id) {
    const url = "/mobile/cart/cartDel";
    let user=JSON.parse(localStorage.getItem("user"))
    const result = API.post(url,{token:user.token,uid:user.user_id,cart_ids:id});
    return result;
};


/**
 * 清空购物车(去除)
 */
// const _cartClear = function() {
//     const url = "/mobile/cart/cartClear";
//     let user=JSON.parse(localStorage.getItem("user"))
//     const result = API.post(url,{token:user.token,uid:user.user_id});
//     return result;
// };



async function _getCarNum() {
    let num=0
    if(localStorage.getItem('user')==null||localStorage.getItem('user')==undefined){
        return num
    }
    await _getUserCart().then(res=>{
        console.log(res)
        if(res.errcode==1){
            for(let i=0;i<res.data.length;i++){
                num+=res.data[i].data.length
            }
        }
    })
    return num
}



/**
 * 购物车结算生成确认订单
 * @param {*} param 
 */
const _confirmOrder=function(param){
    let url="/mobile/cart/confirmOrder";
    const result=API.post(url,param)
    return result
}



export {
    _getUserCart,
    _addCartGoods,
    _changeCartGoodsNum,
    _cartDel,
    // _cartClear,
    _getCarNum,
    _confirmOrder
}